<template>
	<view>
		<template v-if="isHadDataFlag">
			<view class="modItem">
				<view class="modTit">订单信息</view>
				<view class="moCon">
					<order-info currType="confirm" :buyDays="buyDays" :vipEndDate="vipEndDate"></order-info>
				</view>
			</view>
			<view class="modItem">
				<view class="modTit">选择子女</view>
				<view class="moCon">
					<sel-child :childList="childList" :childIndex="childIndex" @selChild="selChild"></sel-child>
				</view>
			</view>
			<bot-btn :actMoney="actMoney" @goPay="goPay" btnTit="确认订单"></bot-btn>
		</template>
		<template v-else-if="isHadDataFlag == false">
			<view class="wholeWh">
				<no-data noDataTxt="请绑定子女"></no-data>
			</view>
		</template>
	</view>
</template>

<script>
	import OrderInfo from '../orderPayZx/components/OrderInfo.vue'
	import SelChild from '../orderPayZx/components/SelChild'
	import BotBtn from '../orderPayZx/components/BotBtn'
	import { getFinalPrice,addStudyBuyOrder } from '@/api/zhuxue'
	export default {
		components:{
			OrderInfo, 
			SelChild, 
			BotBtn
		},
		data(){
			return{
				childList:[],
				childIndex:0,
				actMoney:0,
				stuId:'',
				buyDays:'',
				vipEndDate:'',
				isHadDataFlag:null,
				isClickFlag:true,
				stuGroupId:0
			}
		},
		onLoad(options){
			this.buyDays = options.buyDays;
			this.getFinalPrice('init');
		},
		methods:{
			selChild(index,item){
				this.childIndex = index;
				item.selFlag = true;
				this.stuId = item.stuUserId;
				this.stuGroupId = item.stuGroupId;
				this.getFinalPrice('sel');
			},
			getFinalPrice(opt){
				var field = { userId:this.stuId,buyDays:this.buyDays,opt:opt };
				getFinalPrice(field).then(res=>{
					//console.log(res)
					const tmpRes = res.data.datas;
					this.vipEndDate = tmpRes.vipEndDate;
					this.actMoney = tmpRes.finalPrice;
					this.isHadDataFlag = true;
					if(opt == 'init'){
						this.childList = tmpRes.stuList;
						for(let i in this.childList){
							if(this.childList[i].selFlag){
								this.stuId = this.childList[i].stuUserId;
								this.stuGroupId = this.childList[i].stuGroupId;
							}
						}
					}
				}).catch(err=>{
					this.isHadDataFlag = false;
					console.log(err)
				});
			},
			goPay(){
				if(this.actMoney > 0){
					var field = { toUserId:this.stuId,money:this.actMoney,buyDays:this.buyDays,toUserGroupId:this.stuGroupId };
					if(this.isClickFlag){
						this.isClickFlag = false;
						addStudyBuyOrder(field).then(res=>{
							uni.navigateTo({
								url:'/pages/subIndex/orderPayZx/orderPayZx?orderId=' + res.data.datas + '&fromPage=zxList&vipEndDate=' + this.vipEndDate + '&buyDays=' + this.buyDays,
								success:()=>{
									this.isClickFlag = true;
								}
							})
						}).catch(err=>{
							this.isClickFlag = true;
						});
					}
				}else{
					this.util.showToast('后续开放')
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: $pss-bg-primary;
	}
</style>
<style lang="scss" scoped>
	.modItem{
		background: #fff;
		border-radius: 15rpx;
		padding: 0 20rpx;
		@include respTo(phone){
			margin-bottom: 30rpx;
		}
		@include respTo(pad){
			margin-bottom: 20rpx;
		}
		.moCon{
			padding-bottom: 20rpx;
		}
		.modTit{
			@include respTo(phone){
				height: 90rpx;
				line-height: 90rpx;
				font-size: $font-size32;
			}
			@include respTo(pad){
				height: 60rpx;
				line-height: 60rpx;
				font-size: $padSize20;
			}
			color: $pss-text-color3;
			font-weight: bold;
		}
	}
	.wrap{
		padding: 30rpx 30rpx 0;
	}
</style>